package com.advertisementsystem.Exception;

import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.NoArgsConstructor;
import org.springframework.http.HttpStatus;

@Data
@NoArgsConstructor
@AllArgsConstructor
public class ResponseResult<T> {

    /**
     * 状态码
     */
    private Integer code;

    /**
     * 返回信息
     */
    private String message;

    /**
     * 返回数据
     */
    private T data;

    /**
     * 时间戳
     */
    private long timestamp;

    public ResponseResult(Integer code, String message, T data) {
        this.code = code;
        this.message = message;
        this.data = data;
        this.timestamp = System.currentTimeMillis();
    }

    /**
     * 成功返回结果
     */
    public static <T> ResponseResult<T> success() {
        return new ResponseResult<>(HttpStatus.OK.value(), "操作成功", null);
    }

    /**
     * 成功返回结果
     */
    public static <T> ResponseResult<T> success(T data) {
        return new ResponseResult<>(HttpStatus.OK.value(), "操作成功", data);
    }

    /**
     * 成功返回结果
     */
    public static <T> ResponseResult<T> success(String message, T data) {
        return new ResponseResult<>(HttpStatus.OK.value(), message, data);
    }

    /**
     * 成功返回结果
     */
    public static <T> ResponseResult<T> success(Integer code, String message, T data) {
        return new ResponseResult<>(code, message, data);
    }

    /**
     * 失败返回结果
     */
    public static <T> ResponseResult<T> error() {
        return new ResponseResult<>(HttpStatus.INTERNAL_SERVER_ERROR.value(), "操作失败", null);
    }

    /**
     * 失败返回结果
     */
    public static <T> ResponseResult<T> error(String message) {
        return new ResponseResult<>(HttpStatus.INTERNAL_SERVER_ERROR.value(), message, null);
    }

    /**
     * 失败返回结果
     */
    public static <T> ResponseResult<T> error(Integer code, String message) {
        return new ResponseResult<>(code, message, null);
    }

    /**
     * 失败返回结果
     */
    public static <T> ResponseResult<T> error(Integer code, String message, T data) {
        return new ResponseResult<>(code, message, data);
    }

    /**
     * 参数验证失败返回结果
     */
    public static <T> ResponseResult<T> validateFailed() {
        return new ResponseResult<>(HttpStatus.BAD_REQUEST.value(), "参数验证失败", null);
    }

    /**
     * 参数验证失败返回结果
     */
    public static <T> ResponseResult<T> validateFailed(String message) {
        return new ResponseResult<>(HttpStatus.BAD_REQUEST.value(), message, null);
    }

    /**
     * 未登录返回结果
     */
    public static <T> ResponseResult<T> unauthorized() {
        return new ResponseResult<>(HttpStatus.UNAUTHORIZED.value(), "暂未登录或token已经过期", null);
    }

    /**
     * 未授权返回结果
     */
    public static <T> ResponseResult<T> forbidden() {
        return new ResponseResult<>(HttpStatus.FORBIDDEN.value(), "没有相关权限", null);
    }

    /**
     * 资源不存在返回结果
     */
    public static <T> ResponseResult<T> notFound() {
        return new ResponseResult<>(HttpStatus.NOT_FOUND.value(), "资源不存在", null);
    }

    /**
     * 判断是否成功
     */
    public boolean isSuccess() {
        return HttpStatus.OK.value() == this.code;
    }
}